package com.netbetter.langzheng.plugin.fileManager.widget.toolbar;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SplitToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.TextToolItem;
import com.netbetter.langzheng.plugin.fileManager.contents.CommandEnums;
import com.netbetter.langzheng.plugin.fileManager.contents.PasteStatus;
import com.netbetter.langzheng.plugin.fileManager.context.ChangeListItemEventContext;
import com.netbetter.langzheng.plugin.fileManager.context.CopyFileEventContext;
import com.netbetter.langzheng.plugin.fileManager.context.CutFileEventContext;
import com.netbetter.langzheng.plugin.fileManager.context.EventUtils;
import com.netbetter.langzheng.plugin.fileManager.model.FileModel;
import com.netbetter.langzheng.plugin.fileManager.widget.chart.SpaceChart;
import com.netbetter.langzheng.plugin.fileManager.widget.commandhandle.CommandHandle;
import com.netbetter.langzheng.plugin.fileManager.widget.menu.FileMenu;
import com.netbetter.langzheng.plugin.fileManager.widget.menu.FolderMenu;

/**
 * @author 嘉陵江里捉鱼 E-mail:tang7259@163.com
 * @Date 创建时间：2008-9-7 下午03:59:38 类说明
 */
public class FileToolbar extends LangzhengToolBar {

    private TextToolItem  fileUploadItem;
    private SplitToolItem fileHandleItem;
    private SplitToolItem folderHandleItem;
    private SplitToolItem spaceItem;
    private SplitToolItem helpCenterItem;

    private TextToolItem  folderCreateItem;
    private TextToolItem  pasteItem;
    private CommandHandle createHandleFile;
    private CommandHandle pasteHandleFromCopy;
    private CommandHandle pasteHandleFromCut;
    private SpaceChart    spaceChart;

    public FileToolbar() {
        super();

        fileUploadItem = new TextToolItem("上载");
        fileUploadItem.setIconStyle("icon-menu-show");

        fileHandleItem = new SplitToolItem("可用操作");
        fileHandleItem.setIconStyle("icon-user-add");
        fileHandleItem.setMenu(new FileMenu());

        folderHandleItem = new SplitToolItem("可用操作");
        folderHandleItem.setIconStyle("icon-user-add");
        folderHandleItem.setMenu(new FolderMenu());

        spaceChart = new SpaceChart();
        spaceItem = new SplitToolItem("使用空间") {

            @Override
            protected void onButtonSelect(ButtonEvent be) {
                spaceChart.show();
            }
        };
        spaceItem.setIconStyle("icon-list-items");

        helpCenterItem = new SplitToolItem("帮助中心");
        helpCenterItem.setEnabled(false);
        helpCenterItem.setIconStyle("icon-list-items");

        createHandleFile = new CommandHandle() {

            @Override
            public CommandEnums getCommandType() {
                return CommandEnums.FOLDER_CREATE;
            }

            @Override
            public boolean isNormale() {
                return true;
            }
        };

        folderCreateItem = new TextToolItem("新建目录") {

            @Override
            protected void onButtonSelect(ButtonEvent be) {
                MessageBox box = MessageBox.prompt("创建目录", "");
                final Listener<WindowEvent> listener = new Listener<WindowEvent>() {

                    public void handleEvent(WindowEvent ce) {
                        Dialog dialog = (Dialog) ce.component;
                        TextField<String> text = (TextField<String>) dialog.getFocusWidget();
                        String userData = (String) text.getValue();
                        Button btn = dialog.getButtonPressed();
                        if (btn.getText().equals("Ok")) {
                            if (userData != null) {
                                if (!createHandleFile.handleCreateFolder(userData)) {
                                    MessageBox.alert("提示", "已经存在" + userData + "，不能创建", null);
                                }
                            }
                        }

                    }
                };
                box.addCallback(listener);
            }

        };
        folderCreateItem.setIconStyle("icon-table");

        pasteItem = new TextToolItem("粘贴") {

            @Override
            protected void onButtonSelect(ButtonEvent be) {
                if (pasteHandleFromCopy.isNormale()) {
                    pasteHandleFromCopy.handle();
                } else {
                    FileModel fileModel = CopyFileEventContext.getFileModel();
                    if (fileModel != null) {
                        tipInfo(EventUtils.getPasteStatusByCopy(), fileModel.getName());
                    }
                }
                if (pasteHandleFromCut.isNormale()) {
                    pasteHandleFromCut.handle();
                } else {
                    FileModel fileModel = CutFileEventContext.getFileModel();
                    if (fileModel != null) {
                        tipInfo(EventUtils.getPasteStatusByCut(), fileModel.getName());
                    }
                }
                CutFileEventContext.cleanUp();
                CopyFileEventContext.cleanUp();
                pasteItem.setEnabled(false);

            }

            /**
             * 提示信息
             * 
             * @param status
             * @param name
             */
            private void tipInfo(PasteStatus status, String name) {
                if (status.equals(PasteStatus.IN_SUB_FOLDER)) {
                    MessageBox.alert("提示", "在子目录下，不能拷贝", null);
                } else if (status.equals(PasteStatus.SAME_NAME_IN_FOLDER)) {
                    MessageBox.alert("提示", "已经存在" + name + "，不能拷贝", null);
                } else if (status.equals(PasteStatus.SAME_PARENT)) {
                    MessageBox.alert("提示", "目录一致，不能拷贝", null);
                }
            }
        };
        pasteItem.setEnabled(false);
        pasteItem.setIconStyle("icon-connect");

        /*
         * 来源为拷贝的粘贴
         */
        pasteHandleFromCopy = new CommandHandle() {

            @Override
            public CommandEnums getCommandType() {
                return CommandEnums.FILE_PASTE_BY_COPY;
            }

            @Override
            public boolean isNormale() {
                PasteStatus status = EventUtils.getPasteStatusByCopy();
                return status.equals(PasteStatus.NORMAL);
            }

        };
        /*
         * 来源为剪切的粘贴
         */
        pasteHandleFromCut = new CommandHandle() {

            @Override
            public CommandEnums getCommandType() {
                return CommandEnums.FILE_PASTE_BY_CUT;
            }

            @Override
            public boolean isNormale() {
                PasteStatus status = EventUtils.getPasteStatusByCut();
                return status.equals(PasteStatus.NORMAL);
            }
        };

        add(fileUploadItem);
        add(folderCreateItem);
        add(pasteItem);
        add(new SeparatorToolItem());
        add(folderHandleItem);
        add(fileHandleItem);
        add(new SeparatorToolItem());
        add(spaceItem);
        add(new SeparatorToolItem());
        add(helpCenterItem);
    }

    protected void updateEvent() {
        FileModel fileModel = ChangeListItemEventContext.getFileModel();
        if (fileModel == null) {
            fileHandleItem.hide();
            folderHandleItem.hide();
            return;
        }
        if (!fileModel.getIsDirectory()) {
            fileHandleItem.show();
            folderHandleItem.hide();
        } else {
            fileHandleItem.hide();
            folderHandleItem.show();
        }
        // 更新节点后，同步更新剪贴状态
        if (EventUtils.isExistPasteFileModel()) {
            pasteItem.setEnabled(true);
        } else {
            pasteItem.setEnabled(false);
        }
    }

    @Override
    protected void copyFileEvent() {
        pasteItem.setEnabled(true);

    }

    @Override
    protected void cutFileEvent() {
        pasteItem.setEnabled(true);
    }

}
